

var EXPORTED_SYMBOLS = ["iMacrosProtocolHandler"];

let iMacrosProtocolHandler = (function() {
    "use strict";
    const Cc = Components.classes;
    const Ci = Components.interfaces;
    const Cu = Components.utils;
    const Cr = Components.results;
    function require(url) {
        let scope = {};
        Cu.import(url, scope);
        return scope;
    }
    let {XPCOMUtils} = require("resource://gre/modules/XPCOMUtils.jsm");
    let {Services} = require("resource://gre/modules/Services.jsm");

    return {
        classDescription: "iMacros Protocol Handler",
        classID: Components.ID("{d32d50cf-ec60-4758-b945-44e208f27120}"),
        contractID: "@mozilla.org/network/protocol;1?name=imacros",

        init() {
            let registrar = Components.manager
                .QueryInterface(Ci.nsIComponentRegistrar);
            registrar.registerFactory(
                this.classID, this.classDescription, this.contractID, this
            );
        },

        terminate() {
            let registrar = Components.manager
                .QueryInterface(Ci.nsIComponentRegistrar);
            
            return Promise.resolve().then( 
                () => registrar.unregisterFactory(this.classID, this)
            );
        },

        createInstance(outer, iid) {
            if (outer)
                throw Cr.NS_ERROR_NO_AGGREGATION;
            return this.QueryInterface(iid);
        },

        QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler,
                                               Ci.nsIFactory]),


        protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE |
            Ci.nsIProtocolHandler.URI_NOAUTH |
            Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
        
        
        scheme: "imacros",
        defaultPort: -1,
        
        allowPort(port, scheme) {
            return false;
        },

        newURI(spec, charset, baseURI) {
            var uri = Cc["@mozilla.org/network/simple-uri;1"].
                createInstance(Ci.nsIURI);
            uri.spec = spec;

            return uri;
        },

        newChannel(uri) {
            
            var ios = Cc["@mozilla.org/network/io-service;1"].
                getService(Ci.nsIIOService);

            return ios.newChannel("javascript:void", null, null);
        },
    };

})();
